package com.baizhi.service.impl;

import com.baizhi.service.CmfzUserService;
import com.baizhi.service.UserThreadServcie;
import com.baizhi.util.ThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

@Service
public class UserThreadServcieImpl implements UserThreadServcie {
    @Autowired
    private CmfzUserService cmfzUserService;

    @Override
    public Map selectAllCountThread() throws InterruptedException {
        Map map = new HashMap<>();
//        1.创建计数器
        CountDownLatch countDownLatch = new CountDownLatch(3);
//        2.开启三个任务线程
        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
//                查询注册量
                try {
                    Map map1 = cmfzUserService.selectByDayCount();
                    map.put("count",map1);
//                计数器减一
                    countDownLatch.countDown();
                }catch (Exception e){
                    countDownLatch.countDown();
                }

            }
        });

        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
//                查询男女比例
                List<Map> maps = cmfzUserService.selectBySexCount();
                map.put("sex",maps);
                countDownLatch.countDown();
            }
        });

        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
//                查询地区分布
                List<Map> maps = cmfzUserService.selectByProvince();
                map.put("province",maps);
                countDownLatch.countDown();
            }
        });
//        3.使用await()方法 让主线程等待任务线程执行结束
        countDownLatch.await();
        return map;
    }

    @Override
    public Map selectAllCountFuture() throws Exception {

        Map map = new HashMap<>();

        Future<String> future1 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Map map1 = cmfzUserService.selectByDayCount();
                map.put("count",map1);
//                计数器减一
                return "haha";
            }
        });

        Future<String> future2 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = cmfzUserService.selectBySexCount();
                map.put("sex",maps);
                return "haha";
            }
        });

        Future<String> future3 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = cmfzUserService.selectByProvince();
                map.put("province",maps);
                return "haha";
            }
        });

        if ("haha".equals(future1.get()) && "haha".equals(future2.get()) && "haha".equals(future3.get())){
            return map;
        }

        return map;
    }


    /**
     * 没有优化  串行执行
     * @return
     */
    @Override
    public Map selectAllCount() {
        Map map = new HashMap<>();
//        注册量
        Map map1 = cmfzUserService.selectByDayCount();
//        地区分布
        List<Map> maps = cmfzUserService.selectByProvince();
//        男女比例
        List<Map> maps1 = cmfzUserService.selectBySexCount();
        map.put("sex",maps1);
        map.put("count",map1);
        map.put("province",maps);
        return map;

    }
}
